package com.huike.game.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.huike.game.R;
import com.huike.game.base.Bean20;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Game20FragmentAirFragment extends Fragment {

    private BarChart chart;
    private Context context;
    private List<Bean20> dataList;
    public Game20FragmentAirFragment(List<Bean20> dataList) {
        this.dataList = dataList;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflate = View.inflate(context, R.layout.game20_fragment_air, null);
        chart = (BarChart) inflate.findViewById(R.id.lc_game20_air);

        TextView byId = inflate.findViewById(R.id.tv_game20_fragment_air);

        chart.setDescription("");
        chart.setDrawGridBackground(false);
        chart.setDrawBarShadow(false);

        Legend legend = chart.getLegend();
        legend.setEnabled(false); //取消legend

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(true);

        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setLabelCount(5, false);
        leftAxis.setSpaceTop(20f);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setLabelCount(5, false);
        rightAxis.setSpaceTop(20f);


        // set data
        chart.setData((BarData) generateDataBar(byId));

        // do not forget to refresh the chart
//        holder.chart.invalidate();
        chart.animateY(700);


        return inflate ;
    }

    private BarData generateDataBar(TextView id) {


        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
        int num = dataList.size() <= 20 ? dataList.size() : 20;

        List<Integer> integers = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            entries.add(new BarEntry(dataList.get(i).getAir(), i));
            integers.add(dataList.get(i).getAir());
        }

        Integer min = Collections.min(integers);
        id.setText("过去1分钟空气质量最差值："+ min);

        BarDataSet d = new BarDataSet(entries, "New DataSet ");
        d.setBarSpacePercent(20f);
        d.setColors(ColorTemplate.VORDIPLOM_COLORS);
        d.setHighLightAlpha(255);

        BarData cd = new BarData(getMonths(), d);
        return cd;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    private ArrayList<String> getMonths() {

        ArrayList<String> m = new ArrayList<String>();

        SimpleDateFormat format = new SimpleDateFormat("mm:ss");

        int num = dataList.size() <= 20 ? dataList.size() : 20;

        for (int i = 0; i < num; i++) {
            Long time = dataList.get(i).getTime();
            String format1 = format.format(new Date(time));
            m.add(format1);
        }

        return m;
    }

}
